import { getYMD, getNowFormatDate, getDay, formatGMTDate, sortBy, toPercent } from '../../../assets/js/util.js'

export default {
    data() {
        return {
            // 默认参数 和 URL
            SRC: '',
            DP: {
                p: '',
                f: '',
                uid: ''
            },
            projects: [],
            region: [],
            /*initWidth 的宽*/
            initWidth: '100%',
            // 添加或搜索数据
            ruleForm: {
                IOS: '',
                android: '',
                ldate: '', // 日期
                cost: '', // 成本
                channelName: '', // 渠道名
                agent: '', //代理公司
                platform: '', // 平台
                rebate: '', //返点
                discount: '', //折后成本
                roi: '', // 分成比例
                id: ''
            },
            // 正则
            rules: {},
            uploadShow: false,
            // 分页
            pageSize: 10,
            pageSizes: [10, 20, 50, 100],
            dataTotal: 0,
            currentPage: 1,
            // 表格数据
            tableData: [],
            tablehed: [{ prop: 'id', label: 'id' }, { prop: 'ldate', label: '日期' }, { prop: 'platform', label: '平台' }, { prop: 'agent', label: '代理公司' }, { prop: 'channelName', label: '渠道名称' }, { prop: 'cost', label: '成本' }, { prop: 'rebate', label: '返点' }, { prop: 'discount', label: '折后成本' }, { prop: 'roi', label: '分成比例' }]
        }
    },
    methods: {
        // 通用 成功 与 失败
        successFun(data) {
            this.$message({
                message: data,
                type: 'success'
            });
        },
        errorFun(data) {
            this.$message.error(data);
        },
        ldateFn(o) {
            this.ruleForm.ldata = o
        },
        // 获取数据 获取工程List 以及页面展示数据
        getProjectData() {
            this.projects = [];
            this.$http.post(`${this.SRC}project/list.do`).then(
                function(res) {
                    var data = res.body;
                    if (data["code"] === 0) {
                        for (var i = 0; i < data['msg'].length; i++) {
                            var obj = {
                                label: data["msg"][i]['projectName'],
                                value: data["msg"][i]['id']
                            }
                            this.projects.push(obj);
                        }
                    } else {
                        this.errorFun(data['msg'])
                    }
                }
            )
        },
        // 选择游戏
        projectsChange(val) {
            var p = this.projects;
            for (var i = 0; i < p.length; i++) {
                if (val === p[i].value) {
                    this.ruleForm.gameName = p[i].label;
                    this.ruleForm.gameId = p[i].value;
                }
            }
        },
        uploadForm() {
            this.uploadShow = true;
        },
        uploadHide() {
            this.uploadShow = false;
            this.commonAjax(this.currentPage, this.pageSize);
        },
        commonAjax() {
            console.log(this.ruleForm.ldate)
            var ldate = this.ruleForm.ldate == "" ? '' : formatGMTDate(this.ruleForm.ldate, true, true);
            this.$http.post(`${this.SRC}advcostconfig/list.do?curPage=${this.currentPage}&pageSize=${this.pageSize}&gameId=${this.DP.p}&ldate=${ldate}&cost=${this.ruleForm.cost}&channelName=${this.ruleForm.channelName}&agent=${this.ruleForm.agent}&platform=${this.ruleForm.platform}&rebate=${this.ruleForm.rebate}&discount=${this.ruleForm.discount}&roi=${this.ruleForm.roi}`).then(function(data) {
                var data = data.body;
                if (data.code == 0) {
                    this.tableData = data.msg.data;
                    this.currentPage = data.msg.curPage;
                    this.dataTotal = data.msg.totalSize;
                } else {
                    this.errorFun(data['msg'])
                }
            }).catch(function(res) {})
        },
        upload() {
            this.commonAjax();
        },
        // 分页
        handleSizeChange(o) {
            this.pageSize = o;
            this.commonAjax();
        },
        handleCurrentChange(o) {
            this.currentPage = o;
            this.commonAjax();
        },
        /*设置初始值*/
        _init() {
            this.commonAjax();
            window.addEventListener('resize', function() {
                /*修改表格的宽度*/
                this.initWidth = document.body.clientWidth - 220 + "px";
            }.bind(this))
        },
        // 编辑
        handleEdit(index, row) {
            var that = this;
            console.log(row)
            new Promise(function(resolve, reject) {
                that.$http.post(`${that.SRC}advcostconfig/modify.do?gameId=${that.DP.p}&id=${row.id}&ldate=${row.ldate}&cost=${row.cost}&channelName=${row.channelName}&agent=${row.agent}&platform=${row.platform}&rebate=${row.rebate}&discount=${row.discount}&roi=${row.roi}`).then(function(data) {
                    if (data.body.code == 0) {
                        that.successFun(data.body['msg']);
                    } else {
                        that.errorFun(data.body['msg'])
                    }
                })
            });
        },
        // 删除用户
        handleDelete(index, row) {
            var that = this;
            var p1 = new Promise(function(resolve, reject) {
                that.$http.post(`${that.SRC}advcostconfig/delete.do?id=${row.id}`).then(function(data) {
                    return resolve(data.body)
                })
            });
            p1.then(function(dataJson) {
                that.$http.post(`${that.SRC}advcostconfig/list.do?curPage=${that.currentPage}&pageSize=${that.pageSize}`).then(function(data) {
                    if (dataJson.code == 0 && data.body.code == 0) {
                        that.tableData = data.body.msg.data;
                        that.successFun(dataJson['msg']);
                        that.$refs['ruleForm'].resetFields();
                        that.ruleForm.ldate = '';
                    } else {
                        that.errorFun(dataJson['msg']);
                    }
                })
            })
        },
        addForm(formName, row) {
            var that = this;
            that.$refs[formName].validate((valid) => {
                if (valid) {
                    var p2 = new Promise(function(resolve, reject) {
                        that.$http.post(`${that.SRC}advcostconfig/add.do?gameId=${that.DP.p}&id=${that.ruleForm.id}&ldate=${formatGMTDate(that.ruleForm.ldate,true,true)}&cost=${that.ruleForm.cost}&channelName=${that.ruleForm.channelName}&agent=${that.ruleForm.agent}&platform=${that.ruleForm.platform}&rebate=${that.ruleForm.rebate}&discount=${that.ruleForm.discount}&roi=${that.ruleForm.roi}`).then(function(data) {
                            return resolve(data.body)
                        })
                    });
                    p2.then(function(dataJson) {
                        that.$http.post(`${that.SRC}advcostconfig/list.do?curPage=${that.currentPage}&pageSize=${that.pageSize}`).then(function(data) {
                            if (dataJson.code == 0 && data.body.code == 0) {
                                that.tableData = data.body.msg.data;
                                that.dataTotal = data.body.msg.totalSize
                                that.currentPage = data.body.msg.curPage
                                that.successFun(dataJson['msg']);
                                that.$refs[formName].resetFields();
                            } else {
                                that.errorFun(dataJson['msg']);
                            }
                        })
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        searchForm() {
            this.commonAjax();
        },
        // 表格下载
        excel(tableIndex, sheet) {
            this.downloadExl(this.tablehed, this.tableData, sheet, sheet);
        },
    },
    computed: {
        xlsx() {
            return this.title + '.xlsx'
        }
    },
    mounted() {
        this.SRC = this.$store.getters.src3;
        this._init();
        this.DP.p = localStorage.getItem('ZULONGBIPROJECTID');
        console.log(this.DP.p);
        this.getProjectData();
    }
}